/**
 * 
 */
package com.jm.web.data;

import com.jm.utils.StringHelper;

import net.sf.json.JSONObject;

/**
 * @author yanjie
 *
 * 2011/9/30 下午4:03:11
 */
public class AjaxActionResult extends CallResult{
	protected String strJSCode = "";
	protected String strJSBeforeCode = ""; 
	
	protected JSONObject extInfo = null;
	
	public AjaxActionResult()
	{
		
	}
	
	
	
	/**
	 * 设置执行的脚本
	 * @param strJSCode
	 */
	public void setJSCode(String strJSCode)
	{
		this.strJSCode = strJSCode;
	}
	
	/**
	 * 获取执行的脚本
	 * @return
	 */
	public String getJSCode()
	{
		return this.strJSCode;
	}
	
	/**
	 * 导出JSON字符串
	 * @return
	 */
	public String ToJSONString()
	{
		JSONObject objJSON = new JSONObject();
		FillJSONObject(objJSON);
		
		return objJSON.toString();
	}
	
	
	/**
	 * 填充JSON对象
	 * @param objJSON
	 */
	protected void FillJSONObject(JSONObject objJSON)
	{
		objJSON.put("success", this.nRetCode==1);
		objJSON.put("ret", this.nRetCode);
		objJSON.put("info", this.strErrorInfo);
		objJSON.put("okinfo", this.strOkInfo);
		objJSON.put("warninginfo", this.strWarningInfo);
		objJSON.put("promptinfo", this.strPromptInfo);
		objJSON.put("code", this.strJSCode);
		objJSON.put("bcode", this.strJSBeforeCode);
		
		if(extInfo!=null)
		{
			objJSON.put("extinfo", extInfo);
		}
	}
	
	/**
	 * 附加JS脚本
	 * @param strJSCode
	 */
	public void AppendJSCode(String strJSCode)
	{
		this.strJSCode += strJSCode;
	}
	
	/**
	 * 附加JS脚本（操作之前）
	 * @param strJSCode
	 */
	public void AppendJSBeforeCode(String strJSBeforeCode)
	{
		this.strJSBeforeCode += strJSBeforeCode;
	}

	/**
	 * 获取JS脚本（操作之前）
	 * @return the strJSBeforeCode
	 */
	public String getJSBeforeCode()
	{
		return strJSBeforeCode;
	}

	/**
	 * 设置JS脚本（操作之前）
	 * @param strJSBeforeCode the strJSBeforeCode to set
	 */
	public void setJSBeforeCode(String strJSBeforeCode)
	{
		this.strJSBeforeCode = strJSBeforeCode;
	}
	
	
	
	/**
	 * 设置扩展信息
	 * @param strKey
	 * @param strInfo
	 */
	public void setExtInfo(String strKey,String strInfo)
	{
		if(extInfo==null)
			extInfo = new JSONObject();
		if(extInfo.has(strKey))
		{
			extInfo.remove(strKey);
		}
		extInfo.put(strKey, strInfo);
	}
	
	public void RemoveExtInfo(String strKey)
	{
		if(extInfo==null)
			return;
		
		if(extInfo.has(strKey))
		{
			extInfo.remove(strKey);
		}
	}
	
	@Override
	public void From(CallResult result) {
		if(result instanceof AjaxActionResult){
			AjaxActionResult ajaxResult = (AjaxActionResult)result;
			if(!StringHelper.IsNullOrEmpty(ajaxResult.getJSBeforeCode()))
				this.setJSBeforeCode(ajaxResult.getJSBeforeCode());
			
			if(!StringHelper.IsNullOrEmpty(ajaxResult.getJSCode()))
				this.setJSCode(ajaxResult.getJSCode());
		}
			
		super.From(result);
	}

}
